<template>
    <div>
        <el-card style="width: 50%;margin:auto;margin-top: 30px;">
        <h3 style="text-align: center;margin-bottom: 20px;">修改密码</h3>
            <el-form ref="form" :rules="rules" :model="user" label-width="80px" style="padding-right: 20px;">
                <el-form-item label="原始密码" prop="password">
                    <el-input show-password v-model="user.password" placeholder="原始密码" prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="newPassword">
                    <el-input show-password v-model="user.newPassword" placeholder="请输入新密码" prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="confirmPassWord">
                    <el-input show-password v-model="user.confirmPassWord" placeholder="请确认密码" prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <div style="text-align: center; margin-bottom: 20px">
                    <el-button type="primary" @click="updata()">确认修改</el-button>
                    <!-- <el-button type="primary" @click="resetParam">重置</el-button> -->
                </div>
            </el-form>
        </el-card>
    </div>
</template>

<script>
export default {
    name: "ChangePass",
    data() {
        var validatePassword = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请确认密码'));
            } else if (value !== this.user.newPassword) {
                callback(new Error('两次输入密码不一致!'));
            } else if (this.user.password == this.user.newPassword) {
                callback(new Error('与原密码相同!'));
            }
             else {
                callback();
            }
        };
        return {
            user: JSON.parse(sessionStorage.getItem('CurUser')),
            // centerDialogVisible: false,
            // dialogFormVisible: false,
            rules: {
                password: [
                    { required: true, message: '请输入原始密码', trigger: 'blur' },
                    { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' },
                ],

                newPassword: [
                    { required: true, message: '请输入原始密码', trigger: 'blur' },
                    { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' },
                ],
                confirmPassWord: [
                    { validator: validatePassword, required: true, trigger: 'blur' }
                ],

            }
        }
    },
    methods: {
        // resetParam() {
        //     this.newPassword = ''
        //     this.confirmPassWord = ''
        // },
        updata() {
            this.confirm_disabled = true;
            this.$refs.form.validate((valid) => {
                if (valid) {  //valid成功为true,失败为false
                    //去后台验证用户密码
                    this.user.password = this.user.newPassword
                    //保存当前用户信息到数据库
                    this.$axios.post(this.$httpUrl + '/user/update', this.user).then(res => res.data).then(res => {
                        console.log(res)
                        if (res.code == 200) {
                            //成功更新
                            this.$message.success('修改成功')
                            //跳转到主页
                            this.$router.replace('/');
                        } else {
                            this.$message.error(res.msg)
                        }
                    });
                } 
            });
        },
    }
}
</script>

<style lang="scss" scoped></style>